package com.itboye.jigongbao.fragments

import android.annotation.SuppressLint
import android.content.Intent
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.itboye.jigongbao.R
import com.itboye.jigongbao.activity.HeZhangActivity
import com.itboye.jigongbao.activity.JiGongBillsActivity
import com.itboye.jigongbao.activity.NotifyMessageActivity
import com.itboye.jigongbao.activity.WagesBillActivity
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseFragment
import com.itboye.jigongbao.bean.FinanceBillBean
import com.itboye.jigongbao.retrofit.base.ResultEntity
import com.itboye.jigongbao.retrofit.presenter.UserPrestener
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.HtmlTextUtil
import com.itboye.jigongbao.utils.IsNeedClick
import com.itboye.jigongbao.utils.SPUtils.getSp
import com.itboye.jigongbao.utils.TitleBarUtils
import com.itboye.jigongbao.utils.ToastUtil.ByAlert
import com.itboye.jigongbao.widget.titlebarwidget.RatioRelativeLayout
import com.jauker.widget.BadgeView
import kotlinx.android.synthetic.main.fragment_account_book.*
import java.util.*


/**
 * Created by Administrator on 2017/11/28.
 */
@SuppressLint("ValidFragment")
class AccountBookFragment(private val index: Int) : BaseFragment(), Observer {
    var bean: FinanceBillBean? = null
    var badgeView: BadgeView? = null
    override fun update(p0: Observable?, p1: Any?) {

        var entity = p1 as ResultEntity
        if (entity.code != 0) {
            ByAlert.alert(entity.msg)
        } else {
            when (entity.eventType) {
                UserPrestener.By_JgbFinanceBill_worker_Success -> {
                    bean = entity.data as FinanceBillBean
                    initTopTextValue(bean?.monthly?.debt?.toDouble()!! / 100, bean?.year?.debt?.toDouble()!! / 100)
                }
                UserPrestener.By_JgbFinanceBill_worker_Fail -> {

                }
                UserPrestener.By_JgbFinanceBill_overseer_Success -> {
                    bean = entity.data as FinanceBillBean
                    initTopTextValue(bean?.monthly?.debt?.toDouble()!! / 100, bean?.year?.debt?.toDouble()!! / 100)
                }
                UserPrestener.By_JgbFinanceBill_overseer_Fail -> {
                    ByAlert.alert(entity.msg)
                }
                UserPrestener.By_Message_unreadCount_Success -> {
                    var unreadCount = entity.data as Int
                    badgeView!!.setTargetView(v_actionbar)
                    badgeView!!.textSize = 8f
                    badgeView!!.setBadgeGravity(Gravity.RIGHT or Gravity.TOP)
                    badgeView!!.setBadgeCount(unreadCount)

                }
                UserPrestener.By_Message_unreadCount_Fail -> {
                    ByAlert.alert(entity.msg)
                }

                else -> {
                }
            }
        }
    }


    var txt_hezhang: TextView? = null
    var re_wagesbill: RatioRelativeLayout? = null
    var re_jigongbills: RatioRelativeLayout? = null
    @IsNeedClick
    var tv_actionbar_right: TextView? = null
    var v_actionbar: ImageView? = null
    var role: String? = null
    var userPresenter: UserPrestener? = null
    override fun getLayoutId(): Int {
        return R.layout.fragment_account_book
    }

    //    var index = IndexFragment@ index
    @SuppressLint("SetTextI18n")
    override fun initData() {
        badgeView = BadgeView(activity)
        role = getSp(Const.ROLE) as String?
        userPresenter = UserPrestener(this)
        TitleBarUtils().initTitlebarStyle1(activity, trans_action_bar_account_book,
                if (role.equals(Const.WORKER)) {
                    "工人账本"
                } else {
                    "工头账本"
                }, 0, "", 0, "")
        txt_hezhang!!.setText(if (role.equals(Const.WORKER)) {
            "核账"
        } else {
            "审账"
        })
        v_actionbar?.visibility = View.VISIBLE
        initTopTextValue(0, 0)
    }

    private fun initTopTextValue(monthValue: Any, yearValue: Any) {
        val startPo = 0
        val endPo1 = ("$monthValue").length
        val endPo2 = ("$yearValue").length
        val title1 = "<font color='" + resources.getColor(R.color.main_blue) + "'>$monthValue</font><br/>本月记账收入"
        val title2 = "<font color='" + resources.getColor(R.color.main_blue) + "'>$yearValue</font><br/>今年记账收入"
        HtmlTextUtil.setDifferentSizeForTextView2(startPo, endPo1, title1, txt_yuzhang)
        HtmlTextUtil.setDifferentSizeForTextView2(startPo, endPo2, title2, txt_nianzhang)
    }

    override fun onResume() {
        super.onResume()
        setIdentity()
    }

    /**
     * type:0 年   1：月
     */
    private fun setIdentity() {
        when (role) {
            Const.WORKER -> {
                //工人
                userPresenter!!.By_JgbFinanceBill_worker(MyApplication.instance!!.loginInfo!!.getMobile(), null, null)
            }
            Const.FORCEMAN -> {
                userPresenter!!.By_JgbFinanceBill_overseer(MyApplication.instance!!.loginInfo!!.getMobile(), null, null)

            }

        }

        userPresenter!!.By_Message_unreadCount(MyApplication.instance!!.loginInfo!!.getUid().toString(), null)
    }

    override fun onClick(p0: View?) {
        var intent: Intent = Intent()
        when (p0) {
            txt_hezhang -> {
                intent = Intent(activity, HeZhangActivity::class.java)
            }
            re_wagesbill -> {
                intent = Intent(activity, WagesBillActivity::class.java)
            }
            re_jigongbills -> {
                intent = Intent(activity, JiGongBillsActivity::class.java).putExtra("type", if (role.equals(Const.WORKER)) {
                    "0"
                } else {
                    "1"
                })
            }
            v_actionbar -> {
                intent = Intent(activity, NotifyMessageActivity::class.java)
            }
            else -> {

            }
        }
        startActivity(intent)
    }


}